//取消遮罩
$('.mask').bind('click', $('.hide'), function() {
	$('.mask').css('display', 'none');
	$('.Remarks').css('display', 'none');
});
//改变处理状态
function changeState(val) {
	
	if(val.innerText == '已处理') {
		val.innerHTML = "<a  href='javascript:;' class='red'>未处理<a>";
	} else {
		val.innerHTML = "<a  href='javascript:;' class='blue'>已处理<a>";
	}

}
layui.use('layer', function() {
	var layer = layui.layer;

	
});
var index = 1;
var count = '';

var Remark = '';
var PageSize = 10;
$(function() {
	var index = 1;
	$('.pageNew').text(index); //设置当前页码
	getData(index);

});

//加载首页
$('.homePage').on('click', function() {

	$('.pageNew').text(1); //设置当前页码
	getData(1);
});
//加载下一页
$('.nextPage').on('click', function() {
	PageCount = Math.ceil(count / PageSize);

	if(index == PageCount) {
		index = PageCount;
		getData(index);
	} else {
		index++;
		$('.pageNew').text(index); //设置当前页码

		getData(index);
	}

});
//加载上一页
$('.prePage').on('click', function() {

	if(index == 1) {
		index = 1;
		$('.pageNew').text(index);
		getData(index);
	} else {
		index--;
		$('.pageNew').text(index); //设置当前页码
		getData(index);
	}

}); //加载尾页
$('.lastpage').on('click', function() {

	PageCount = Math.ceil(count / PageSize);
	index = PageCount;
	$('.pageNew').text(PageCount); //设置当前页码
	getData(PageCount);
});
//得到数据
function getData(index) {
	$('#applyTable').empty();

	$.ajax({
		async: true,

		type: "get",
		data: {
			index: index
		},
		url: "/api/apply/v1/getapply.action",
		success: function(data) {

			if(data.code == 0) {

				var str = data.applylist.list;
				for(var i = 0; i < str.length; i++) {
					var html = `<tr>  
								                    <td>${str[i].applyAid}</td>
													<td>${str[i].userPhone}</td>
													<td>${str[i].applyType==1 ?'车贷':str[i].applyType ==2?'房贷':str[i].applyType ==3?'预测':'未知'}</td>
													<td> ${str[i].applyCreationTime}</td>
													<td  onclick='changeState(this),modifyState1("${str[i].applyAid}","${str[i].applyState}")'>${str[i].applyState ==1 ?'<a  href="javascript:;" class="red">未处理</a>' :str[i].applyState==2 ? '<a  href="javascript:;" class="blue">已处理</a>':'<a class="blue"  href="javascript:;">未知</a>'}</td>
													<td>
														<a style='color:#527ef0' href='javascript:;' onclick='Remarks("${str[i].applyAid}")'>${str[i].applyRemarks  == '' ? '无' : str[i].applyRemarks}</a>
													</td>
													<td>
														<div onclick='showDetail("${str[i].applyAid}","${str[i].applyType}")'>
															<a style='color:cornflowerblue' href='javascript:;'>详情</a>
														</div>
													</td>
												</tr>`;

					$('#applyTable').append(html);
					count = data.applylist.count;
					var PageCount = Math.ceil(count / PageSize);
					//设置尾页
					$('.pageCount').text(PageCount);

				}

			}
		}
	});
};

//点击进入详情
function showDetail(apid, atype) {
	$('.mask-center').empty();
	if(atype == 3) {

		$.ajax({
			async: true,

			type: "post",
			data: {
				apid: apid,
				atype: atype
			},
			url: "/api/apply/v1/getdetails.action",
			success: function(data) {

				if(data.code === 0 && data.forecasts != null) {
					$('.mask').css('display', 'block');
					//
					var html = `
		         <div class="detail">
					<div class="form-inline detail-list">
						<p>所需金额：</p>
						<p>${data.forecasts.forecastNeedMoney}</p>
                    </div>
                 </div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>性别：</p>
						<p>${data.forecasts.forecastSex==1 ?'男':data.forecasts.forecastSex==2?'女':'未知'}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>姓名：</p>
						<p>${data.forecasts.forecastName}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>身份特征：</p>
						<p>${data.forecasts.forecastType==1 ?'自由职业':data.forecasts.forecastType==2?'上班族':data.forecasts.forecastType==3?'公务员':data.forecasts.forecastType==4?'企业主':data.forecasts.forecastType==5?'个体户':'未知'}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>月收入：</p>
						<p>${data.forecasts.forecastMoney}</p>

					</div>

				</div>
				
				<div class="detail">
					<div class="form-inline detail-list">
						<p>房产情况：</p>
						<p>${data.forecasts.forecastHouse==0?'无':data.forecasts.forecastHouse==1?'有':'未知'}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>车产情况</p>
						<p>${data.forecasts.forecastCar==0?'无':data.forecasts.forecastCar==1?'有':'未知'}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>公积金情况：</p>
						<p>${data.forecasts.forecastAf==0?'无':data.forecasts.forecastAf==1?'有':'未知'}</p>

					</div>

				</div>
				<div class="detail">
					<div class='form-inline detail-list' style="margin: 10px 0 0 0;">
						<button class='btn btn-primary hid'> 确定</button>
						<button class='btn btn-danger hid'> 取消</button>

					</div>

				</div>
						`
					$('.mask-center').append(html);
				} else {
					layer.msg('暂无详情！');
				}

			},
			errror: function() {
				layer.msg('失败的操作！');
			}
		});

	} else if(atype == 1) {
		$('.mask-center').css('height', '514px');

		$.ajax({
			async: true,

			type: "post",
			data: {
				apid: apid,
				atype: atype
			},
			url: "/api/apply/v1/getdetails.action",
			success: function(data) {
				
				if(data.code === 0 && data.carlists != null) {
					$('.mask').css('display', 'block');
					//
					var html = `
		         <div class="detail">
					<div class="form-inline detail-list">
						<p>车辆品牌：</p>
						<p>${data.carlists.carBrand}</p>
                    </div>
                 </div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>车辆系列：</p>
						<p>${data.carlists.carSeries}</p>

					</div>

				</div>
				
				<div class="detail">
					<div class="form-inline detail-list">
						<p>车辆上牌时间：</p>
						<p>${data.carlists.carUpIdTime}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>车辆行驶里程：</p>
						<p>${data.carlists.carKmNumber}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>车商零售价：</p>
						<p>${data.carlists.carB2c}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>车商收购价：</p>
						<p>${data.carlists.carC2b}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>个人交易价：</p>
						<p>${data.carlists.carC2c}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p>车辆指导价：</p>
						<p>${data.carlists.carCost}</p>

					</div>

				</div>
				<div class="detail">
					<div class="form-inline detail-list">
						<p style="margin-left:40px">创建时间：</p>
						<p>${data.carlists.carCreationTime}</p>

					</div>

				</div>
				
				<div class="detail">
					<div class='form-inline detail-list' style="margin: 10px 0 0 0;">
						<button class='btn btn-primary hid'> 确定</button>
						<button class='btn btn-danger hid'> 取消</button>

					</div>

				</div>
						`
					$('.mask-center').append(html);
				} else {
					layer.msg('暂无详情！');
				}

			},
			errror: function() {
				layer.msg('失败的操作！');
			}
		});

	} else {
		layer.msg('暂无详情！');
	}

};
//添加备注
function Remarks(id) {
	Remark = id;
	$('.Remarks').css('display', 'block');
	//			 

};

//修改备注
function modifyState() {
	//				$('#reInput').val("");
	var applyremarks = $('#reInput').val();

	$.ajax({
		async: true,

		type: "post",
		data: {
			apid: Remark,
			applyremarks: applyremarks

		},
		url: "/api/apply/v1/updatestate.action",
		success: function(data) {

			$('#reInput').val(" ");
			if(data.code == 0) {
				layer.msg('备注修改成功！');
				//							alert('备注修改成功！');
				$('.Remarks').css('display', 'none');

				getData(1);
			} else {
				layui.msg('备注修改失败！');
				$('.Remarks').css('display', 'none');
			}
		}
	});
}
//修改状态
function modifyState1(id, val) {

	$.ajax({
		async: true,

		type: "post",
		data: {
			apid: id,
			state: val == 1 ? 2 : 1

		},
		url: "/api/apply/v1/updatestate.action",
		success: function(data) {

			//					
			if(data.code == 0) {

				layui.msg('状态修改成功！');

			} else {
				layui.msg('状态修改失败！');

			}
		}
	});
}